home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / RelativeURL.java < prev    next >
Text File  |  1998-08-21  |  4KB  |  119 lines

  1. package symantec.itools.net;
  2.  
  3.  
  4. import java.net.URL;
  5. import java.net.MalformedURLException;
  6. import symantec.itools.lang.Context;
  7. import java.io.*;
  8.  
  9.  
  10. //  05/15/97    CAR Added ieAnchorHack to work around an IE problem with anchor symbols in URLs.
  11.  
  12. /**
  13.  * This class is used in conjunction with class symantec.itools.OS.Context to
  14.  * provide URLs that are relative to an appletÆs or applicationÆs
  15.  * document base. For applets the document base is the URL of the document
  16.  * that the applet is embedded in. For applications the document base is the
  17.  * same as the ôuser.dirö system property.
  18.  */
  19. public class RelativeURL
  20. {
  21.     /**
  22.      * DonÆt use, this is an all-static class.
  23.      */
  24.     public RelativeURL() {
  25.     }
  26.  
  27.     /**
  28.      * Determines the absolute URL given a relative URL.
  29.      * If the spec parameter is relative, it is considered to be relative
  30.      * to the current document base as determined by getDocumentBase() in
  31.      * class symantec.itools.lang.Context.
  32.      *
  33.      * @param spec a possibly relative URL
  34.      * @return the absolute URL equivalent to the given relativeURL
  35.      * @exception MalformedURLException
  36.      * if cannot generate the resultant URL due to a bad spec parameter
  37.      * or a bad document base
  38.      */
  39.     public static URL getURL(String spec)
  40.         throws MalformedURLException
  41.     {
  42.  
  43.         // InternetExplorer 3 for some reason strips out a single anchor symbol (#)
  44.         // when the URL is passed to showDocument() so we double up the symbol (##)
  45.         if (System.getProperty("java.vendor").startsWith("Microsoft") &&
  46.             System.getProperty("java.version").startsWith("1.0") &&
  47.             spec.indexOf('#') > -1) {
  48.                 spec = ieAnchorHack(spec);
  49.         }
  50.  
  51.         URL documentBase = Context.getDocumentBase();
  52.  
  53.         if(documentBase != null && spec.indexOf("//") == -1)
  54.         {
  55.             return new URL(documentBase,spec);
  56.         }
  57.  
  58.         return new URL(spec);
  59.     }
  60.  
  61.     private static String ieAnchorHack(String spec) {
  62.         String str = spec.substring(0, spec.lastIndexOf('#'));
  63.         str += "#";
  64.         str += spec.substring(spec.lastIndexOf('#'));
  65.         return str;
  66.     }
  67.  
  68.     /**
  69.      * Attempts to load an object from a .SER file.
  70.      *
  71.      * @param Class the class to be created
  72.      * @param serName the name of the serialization file
  73.      * @return the object created (or NULL, if any error occurred)
  74.      */
  75.     public static Object loadObject(Class c, String serName) {
  76.         Object ro = null;
  77.  
  78.         java.io.InputStream ins;
  79.         ClassLoader loader;
  80.  
  81.         loader = c.getClassLoader();
  82.         if (loader == null)
  83.             ins = ClassLoader.getSystemResourceAsStream(serName);
  84.         else
  85.             ins = loader.getResourceAsStream(serName);
  86.  
  87.         if (ins == null) {
  88.             try {
  89.                 ins = new FileInputStream(serName);
  90.             } catch (Exception e) {
  91.             }
  92.         }
  93.  
  94.         if (ins != null) {
  95.             try {
  96.                 ObjectInputStream ois = new ObjectInputStream(ins);
  97.                 ro = ois.readObject();
  98.                 ois.close();
  99.             } catch (Exception e) {
  100.             }
  101.  
  102.             try {
  103.                 ins.close();
  104.             } catch (Exception e) {
  105.             }
  106.         }
  107.  
  108.         if (ro == null) {
  109.             try {
  110.                 ro = c.newInstance();
  111.             } catch (Exception e) {
  112.             }
  113.         }
  114.  
  115.         return ro;
  116.     }
  117.  
  118. }
  119.